<?php

/**
 * This is the model class for table "tbl_lab".
 *
 * The followings are the available columns in table 'tbl_lab':
 * @property integer $lab_id
 * @property integer $user_id
 * @property integer $labtype_id
 * @property string $lab_name
 * @property string $description
 * @property string $create_date
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Labtype $labtype
 */
class Lab extends CActiveRecord
{
    const STATUS_ACTIVE = 1;
    const STATUS_DEACTIVE = 0;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Lab the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_lab';
	}
    public function getLabTypeText()
    {
        if($this->status == 1)
        {
            return "软件实验室";
        }
        if($this->status == 1)
        {
            return "硬件实验室";
        }
        if($this->status == 1)
        {
            return "嵌入式实验室";
        }
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, labtype_id, lab_name, image', 'required'),
			array('user_id, labtype_id, status', 'numerical', 'integerOnly'=>true),
			array('lab_name', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('lab_id, user_id, labtype_id, lab_name, description, create_date, status', 'safe', 'on'=>'search'),
		);
	}
    public function getLabTypeOptions()
    {
        $model = Labtype::model()->findAll("status = 1");
        $data = array();
        foreach($model as $elem)
        {
            $data[$elem->labtype_id] = $elem->title;
        }
        return $data;
    }
    public function getStatusOptions()
    {
        return array (
            self::STATUS_ACTIVE => 'ACITVE',
            self::STATUS_DEACTIVE => 'DEACTIVE',
            );
    }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'labtype' => array(self::BELONGS_TO, 'Labtype', 'labtype_id'),
                                                                'picture' => array(self::BELONGS_TO, 'Resources', 'image'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'lab_id' => 'Lab',
			'user_id' => 'User',
			'labtype_id' => 'Labtype',
			'lab_name' => 'Lab Name',
			'description' => 'Description',
			'create_date' => 'Create Date',
			'status' => 'Status',
                                                                'image' => 'Image',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('lab_id',$this->lab_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('labtype_id',$this->labtype_id);
		$criteria->compare('lab_name',$this->lab_name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
